//
//  YHBar.swift
//  XLWB
//
//  Created by key on 16/6/25.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class YHBar: UITabBar {
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        addSubview(centerBtn)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 布局子控件
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        //每个标签的宽度
        let barW = screenW / 5
        
        //标签下标
        var index : CGFloat = 0
        
        centerBtn.center.x = self.center.x
        
        centerBtn.center.y = self.bounds.size.height / 2
        
        //循环获取获取子控件
        for vw in subviews {
            
            //判断子控件的类型
            if vw.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                vw.frame.size.width = barW
                
                vw.frame.origin.x = index * barW
                
                if index == 1 {
                    
                    index++
                }
                
                index++
                
            }
            
        }
    }
    
    
    //MARK: - 创建中间按钮 私有的懒加载 一个控件
    private lazy var centerBtn : UIButton = {
        
        let button = UIButton()
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
        
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return button
        
    }()
    
    
    //MARK: - 监听按钮
    func buttonClick() {

        //调用闭包
        closBag?()
    }
    
    //MARK: - 声明一个闭包
    var closBag : (() -> ())?
    
}
